﻿using B.S.AuthCenter.Api.Read.Application.Command.Roles;
using B.S.AuthCenter.Api.Read.DTOs;
using B.S.AuthCenter.Domain;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;

namespace B.S.AuthCenter.Api.Read.Application.Handler.Roles
{
    public class GetRolesHandler : IRequestHandler<GetRolesCommand, ApiResult<RolesDTO>>
    {
        IBaseRepository<RoleModel> roleRepo;

        public GetRolesHandler(IBaseRepository<RoleModel> roleRepo)
        {
            this.roleRepo = roleRepo;
        }

        public Task<ApiResult<RolesDTO>> Handle(GetRolesCommand request, CancellationToken cancellationToken)
        {
            ApiResult<RolesDTO> res=new ApiResult<RolesDTO>();
            var roles=roleRepo.GetAll().Where(c=>!c.IsDeleted);
            var linq = from r in roles
                       select new RolesDTO
                       {
                           Id = r.Id,
                           RoleName=r.RoleName,
                           Creator=r.Creator,
                           CreatorDate=r.CreatorDate,
                           Updator=r.Updator,
                           UpdatorDate=r.UpdatorDate,
                       };
            res.Msg = "查询角色列表成功";
            res.Code = ApiEnum.Success;
            res.Data=linq.OrderByDescending(x=>x.Id).ToList();
            return Task.FromResult(res);
        }
    }
}
